﻿package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.display.Graphics;
	import flash.geom.Point;
	import flash.display.GradientType;
	/**
	 * 反射倒影生成类...
	 * @copy siweiZhu ...
	 * the4v.com
	 */
    public class SWreflection extends Sprite{
        private var _reflection:Bitmap;
		private var _bit:BitmapData;
		private var _mtx:Matrix;
		private var _rBox:Shape;
		private var _Alpha:BitmapData;
		private var _$Height:Number;
		private var _$Alpha:Number;
		/**
		 * 构造
		 * @param	$Target 目标对象，即要添加投影的对象
		 * @param	$Height 投影高度，即投影从出现到虚化的长度
		 * @param	$Alpha 投影透明度
		 */
        public function SWreflection($Target:DisplayObject, $Height:Number = 200, $Alpha:Number = 1):void  {
			_$Height = $Height;
			_$Alpha = $Alpha;
			updateTarget($Target);
        }
		public function updateTarget($Target:DisplayObject):void
		{
			if (_reflection) {
			    _reflection.bitmapData.dispose();
				if (contains(_reflection))
				    removeChild(_reflection);
			}
			_reflection = new Bitmap(getReflection($Target,_$Height));//
			_reflection.smoothing = true ;//平滑
			_reflection.alpha = _$Alpha;
			addChild(_reflection); 
			this.x = $Target.x;
			this.y = $Target.y + $Target.height;
		}
        /**
         * 生成投影
         * @param	target 目标
         * @param	Height 高度
         * @return
         */
        private function getReflection(target:DisplayObject, Height:Number = -1):BitmapData {
			if (Height < 0) Height = target.height ;
			_bit = new BitmapData(target.width, Height, true, 0); 
			_bit.draw(target, new Matrix(1, 0, 0, -1, 0, target.height));  
			_mtx = new Matrix(); 
			_mtx.createGradientBox(target.width, Height, 0.5 * Math.PI); 
			_rBox = new Shape(); 
			_rBox.graphics.beginGradientFill(GradientType.LINEAR, [0, 0], [0.5, 0], [0, 255], _mtx)  
			_rBox.graphics.drawRect(0, 0, target.width, Height);  _rBox.graphics.endFill();  
			_Alpha = new BitmapData(target.width, Height, true, 0); 
			_Alpha.draw(_rBox); 
			_bit.copyPixels(_bit, _bit.rect, new Point(0, 0), _Alpha, new Point(0, 0), false); 
			return _bit.clone();
		}
    }
}
